<?php
class ControllerSaleMailTask extends AdminController {

	public function index() {
		M('sale/mail_task', 'mod_task');
		$this->getList();
	}
	
	public function insert() {
		M('sale/mail_task', 'mod_task');
		if ($this->request->isPost()) {
			$this->mod_task->addMailTask($this->request->post);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/mail_task'));
		}
		$this->getForm();
	}
	
	public function update() {
		M('sale/mail_task', 'mod_task');
		if ($this->request->isPost()) {
			$this->mod_task->editMailTask($this->request->get['mail_task_id'], $this->request->post);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/mail_task'));
		}
		$this->getForm();
	}
	
	public function delete() {
		M('sale/mail_task', 'mod_task');
		if ($this->request->post['selected']) {
			$this->mod_task->deleteMailTask($this->request->post['selected']);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/mail_task'));
		}
		$this->getList();
	}
	
	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
	
		$qf = new Query_filter();
		$total = $this->mod_task->getTotalMailTasks($qf);
		$this->data['tasks'] = $this->mod_task->getMailTasks($qf);
		$this->data['pagination'] = $this->pagination($total)->render();
	
		$this->data['filter_title'] = $qf->get('filter_title');
	
		$this->display('sale/mail_task_list.tpl');
	}
	
	protected function getForm() {
		$this->document->addScript(ROOT . 'static/javascript/jquery/jquery.validate.js');
		$this->document->setTitle($this->_['heading_title']);
		
		$mail_task_id = G('mail_task_id');
		if ($mail_task_id) {
			$this->data['action'] = UA('sale/mail_task/update', "mail_task_id=$mail_task_id");
			if (!$this->request->isPost()) {
				$this->vo->data = $this->mod_task->getMailTask($mail_task_id);
			}
		}
		else {
			$this->data['action'] = UA('sale/mail_task/insert');
		}
	
		M('setting/store');
		$this->data['stores'] = $this->model_setting_store->getStores();
		
		M('sale/customer_group');
		$this->data['customer_groups'] = $this->model_sale_customer_group->getCustomerGroups(0);
		
		$this->display('sale/mail_task_form.tpl');
	}

	public function send() {
		if (!$this->hasMP()) {
			$json['error'] = $this->_['error_permission'];
			
		}
		else {
			M('sale/mail_task', 'mod_task');
			set_time_limit(600);
			$rs = $this->mod_task->send($this->request->get['mail_task_id']);
			if ($rs == false) {
				$json['error'] = $this->_['error_mail'];
			}
			else {
				$json['message'] = sprintf($this->_['text_mail_sent'], $rs['sent'], $rs['items']);
				$json['finished'] = ($rs['sent'] >= $rs['items']) ? 1 : 0;
			}
		}
		$this->response->setOutput(json_encode($json));
	}
	
	public function items() {
		$this->document->setTitle($this->_['heading_title']);
		
		M('sale/mail_task', 'mod_task');
		
		$qf = new Query_filter(null, false);
		$total = $this->mod_task->getTotalTaskItems($qf);
		$this->data['items'] = $this->mod_task->getTaskItems($qf);
		
		$url = UA('sale/mail_task/items', 'filter_mail_task_id='. $qf->get('filter_mail_task_id') .'&page={page}');
		$page = $qf->get('page', 1);
		$this->data['pagination'] = $this->pagination($total, $url, null, $page)->render();
		
		$this->display('sale/mail_task_items.tpl');
	}
}
?>